package com.fzn3.day02;

import org.junit.Test;
import org.junit.jupiter.api.DisplayName;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;

/**
 * @program: DataStructure
 * @author: fzn
 * 描述：  动态数组
 * @create: 2023/3/8 16:16
 **/
public class DynamicArrayTest {


    @Test
    @DisplayName(("添加方法和for循环遍历"))
    public void test1() {
        DynamicArray dynamicArray = new DynamicArray();
        dynamicArray.addList(1);
        dynamicArray.addList(2);
        dynamicArray.addList(3);
        dynamicArray.addList(4);
        dynamicArray.addList(5);
        dynamicArray.add(2, 6);

        dynamicArray.foreach((element) -> {
            System.out.print(element + " ");
        });
        System.out.println();
    }

    @Test
    @DisplayName(("迭代器遍历"))
    public void test2() {
        DynamicArray dynamicArray = new DynamicArray();
        dynamicArray.addList(1);
        dynamicArray.addList(2);
        dynamicArray.addList(3);
        dynamicArray.addList(4);
        dynamicArray.addList(5);

        for (Integer element : dynamicArray) {
            System.out.print(element + " ");
        }
        System.out.println();
    }

    @Test
    @DisplayName(("流的方式遍历"))
    public void test3() {
        DynamicArray dynamicArray = new DynamicArray();
        dynamicArray.addList(1);
        dynamicArray.addList(2);
        dynamicArray.addList(3);
        dynamicArray.addList(4);
        dynamicArray.addList(5);

        dynamicArray.stream().forEach(element -> {
            System.out.print(element + " ");
        });
        System.out.println();
    }

    @Test
    @DisplayName(("删除"))
    public void test4() {
        DynamicArray dynamicArray = new DynamicArray();
        dynamicArray.addList(1);
        dynamicArray.addList(2);
        dynamicArray.addList(3);
        dynamicArray.addList(4);
        dynamicArray.addList(5);
        // 删除
        int removed = dynamicArray.remove(2);
        assertEquals(3, removed);
        assertIterableEquals(Arrays.asList(1, 2, 4, 5), dynamicArray);
    }

    @Test
    @DisplayName(("扩容"))
    public void test5() {
        DynamicArray dynamicArray = new DynamicArray();
        for (int i = 0; i < 9; i++) {
            dynamicArray.addList(i + 1);
        }

        assertIterableEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9), dynamicArray);
    }


}
